package com.dongdongmovie.controller;

import com.dongdongmovie.pojo.AdminUser;
import com.dongdongmovie.pojo.Role;
import com.dongdongmovie.service.AdminUser_Service;
import com.dongdongmovie.service.RoleResource_Service;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("AdminUser")
public class AdminUser_Controller {

    //注入service
    @DubboReference
    private AdminUser_Service adminUser_service;
    @DubboReference
    private RoleResource_Service resource_service;

    //跳转到show
    @RequestMapping("tzshow")
    public String tzshow() {
        return "AdminUser/show";
    }

    //跳转到add
    @RequestMapping("tzadd")
    public String tzadd(Model m) {
        List<Role> show = resource_service.show();
        m.addAttribute("role", show);
        return "AdminUser/add";
    }

    //登录
    @ResponseBody
    @RequestMapping("login")
    public Map<String, Object> login(AdminUser a,HttpServletRequest RequestMapping) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        Map<String, Object> updateshowlogin = adminUser_service.updateshowlogin(a);
        if ((boolean) updateshowlogin.get("code")) {
            AdminUser aa = (AdminUser)updateshowlogin.get("AdminUser");
            RequestMapping.getSession().setAttribute("AdminUser",aa);
        }
        return updateshowlogin;
    }

    //退出登录
    @RequestMapping("exitlogin")
    public String exitlogin(HttpServletRequest request) {
        request.getSession().removeAttribute("AdminUser");
        return "index.html";
    }


    //登陆后进入主页
    @RequestMapping("tzResource")
    public String tzResource() {
        return "Resource/resource";
    }

    //跳转到update
    @RequestMapping("tzupdate")
    public String tzupdate(Integer userId, Model m) {
        AdminUser a = adminUser_service.showbyId(userId);
        m.addAttribute("a", a);
        List<Integer> list = adminUser_service.showroleids(userId);
        m.addAttribute("list", list);
        List<Role> show = resource_service.show();
        m.addAttribute("role", show);
        return "AdminUser/update";
    }

    //查寻数据
    @ResponseBody
    @RequestMapping("show")
    public List<AdminUser> show() {
        return adminUser_service.show();
    }

    //添加数据
    @ResponseBody
    @RequestMapping("add")
    public int add(AdminUser a, Integer[] roleIds) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        return adminUser_service.add(a, roleIds);
    }

    //修改数据
    @ResponseBody
    @RequestMapping("update")
    public int update(AdminUser a,Integer[] roleIds) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        return adminUser_service.update(a, roleIds);
    }

    //删除数据
    @ResponseBody
    @RequestMapping("delete")
    public int delete(Integer userId) {
        return adminUser_service.delete(userId);
    }

}
